package edu.ustb.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import edu.ustb.domain.Goods;
import edu.ustb.mapper.CartMapper;
import edu.ustb.mapper.GoodsMapper;
import edu.ustb.utils.FileUtil;


@WebServlet("/deleteCart.do")
@MultipartConfig//servlet3.0支持文件上传
public class CartGoodsDeleteController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String cartId = request.getParameter("cartId");
	
		try{	
			InputStream  is = Resources.getResourceAsStream("mybatis.xml");
			SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
			SqlSession openSession = ssf.openSession(true);//事物的自动提交
			CartMapper mapper = openSession.getMapper(CartMapper.class);
			if (cartId!=null){
				//	单个商品点击对应的删除键删除
				int rows=mapper.removeCart(Integer.parseInt(cartId));			
				if (rows>0){
					request.getRequestDispatcher("/cart.action").forward(request, response);				
				}	
			}
//			if (checkbox_arr!=null){
//			//多个选中复选框的商品的删除
//				for (int i=0;i<checkbox_arr.length;i++){
//					String checkbox_id=checkbox_arr[i];
//					System.out.println(checkbox_id);
//					int rows1=mapper.delete(Integer.parseInt(checkbox_id));
//				}
//					request.getRequestDispatcher("/Cart.jsp").forward(request, response);
//			}
			}catch (Exception e) {
				e.printStackTrace();
			}		
	}
}
